package com.dodoke.controller;

import java.util.List;

import com.dodoke.common.core.domain.AjaxResult;
import com.dodoke.domain.WxAddresss;
import com.dodoke.service.IWxAddresssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dodoke.common.core.controller.BaseController;
import com.dodoke.common.core.page.TableDataInfo;

/**
 * 【地址】Controller
 *
 * @author dodoke
 * @date 2020-08-25
 */
@Api("地址模块")
@RestController
@RequestMapping("/addresss")
public class WxAddresssController extends BaseController
{
    @Autowired
    private IWxAddresssService wxAddresssService;


    /***
     * 查询【地址】列表
     * @param openId
     * @return
     */
    @RequestMapping("/list/{openId}")
    @ApiOperation("获取用户的地址列表")
    @ApiImplicitParam(name = "openId",value = "用户身份唯一标识",required = true,paramType = "path")
    public TableDataInfo list(@PathVariable String openId)
    {
        startPage();
        List<WxAddresss> list = wxAddresssService.selectWxAddresssList(openId);
        return getDataTable(list);
    }

    /***
     * 查询默认地址
     * @param openId
     * @return
     */
    @RequestMapping("/selectDefaultAddressByOpenId/{openId}")
    @ApiOperation("获取用户的默认地址")
    @ApiImplicitParam(name = "openId",value = "用户身份唯一标识",required = true,paramType = "path")
    public AjaxResult selectDefaultAddressByOpenId(@PathVariable String openId) {
        WxAddresss wxAddresss = wxAddresssService.selectDefaultAddressByOpenId(openId);
        return AjaxResult.success(wxAddresss);
    }

    @RequestMapping("/saveAddress/{openId}")
    @ApiOperation("存储用户地址")
    @ApiImplicitParam(name = "openId",value = "用户身份唯一标识",required = true,paramType = "path")
    public AjaxResult saveAddressByOpenId(@PathVariable String openId,WxAddresss wxAddresss) {
        //根据openId查询用户的默认地址
        WxAddresss wxAddresss1 = wxAddresssService.selectDefaultAddressByOpenId(openId);
        if(wxAddresss.getIsDefault() == 1) {
            //将该openId的用户地址全部变为0-->不默认
            wxAddresssService.updateAddressByOpenId(openId);
            //通过openId获取id 对应 地址表的wx_id
            int wxId = wxAddresssService.getUserIdByOpenId(openId);
            wxAddresss.setWxId((long) wxId);
            //然后在进行插入操作
            wxAddresssService.insertAddressInfo(wxAddresss);
        } else { // 不存在默认地址 直接插入
            //通过openId获取id 对应 地址表的wx_id
            int wxId = wxAddresssService.getUserIdByOpenId(openId);
            wxAddresss.setWxId((long) wxId);
            //然后在进行插入操作
            wxAddresssService.insertAddressInfo(wxAddresss);
        }
        return AjaxResult.success(wxAddresss);
    }

    @RequestMapping("/deleteAddressById/{id}")
    @ApiOperation("删除对应的地址")
    @ApiImplicitParam(name = "id",value = "地址对应的标识",required = true,paramType = "path")
    public AjaxResult deleteAddressById(@PathVariable Long id) {
        int i = wxAddresssService.deleteAddressById(id);
        return toAjax(i);
    }

    @RequestMapping("/selectAddressInfoById/{id}")
    @ApiOperation("查询对应的地址信息")
    @ApiImplicitParam(name = "id",value = "地址对应的标识",required = true,paramType = "path")
    public AjaxResult selectAddressInfoById(@PathVariable Long id) {
        WxAddresss wxAddresss = wxAddresssService.selectAddressById(id);
        return AjaxResult.success(wxAddresss);
    }

    @RequestMapping("/updateAddress/{id}")
    @ApiOperation("更新对应的用户地址")
    @ApiImplicitParam(name = "id",value = "地址对应的标识",required = true,paramType = "path")
    public AjaxResult updateAddressById(@PathVariable Long id, String openId,WxAddresss wxAddresss) {
        //假如传过来的 编辑为默认地址
        if(wxAddresss.getIsDefault() == 1) {
            //将该openId的用户地址全部变为0-->不默认
            wxAddresssService.updateAddressByOpenId(openId);
            //完善地址对象
            wxAddresss.setId(id);
            //将前端传来的编辑后封装的对象传到更新的方法中
            int i = wxAddresssService.updateAddressById(wxAddresss);
            return toAjax(i);

        } else { // 假如传过来的 不编辑为默认地址 直接进行更新操作
            //完善地址对象
            wxAddresss.setId(id);
            //将前端传来的编辑后封装的对象传到更新的方法中
            int i = wxAddresssService.updateAddressById(wxAddresss);
            return toAjax(i);
        }
    }

}
